﻿using System;
using System.Windows;
using System.Windows.Input;
using SSLVerifier.API.MainLogic;

namespace SSLVerifier.API.ViewModels {
	class AddServerVM : DependencyObject {
		public ICommand OkCommand {
			get { return new RelayCommand(AddServer, CanAddServer); }
		}

		public static readonly DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName", typeof(String), typeof(AddServerVM), new PropertyMetadata(String.Empty));
		public static readonly DependencyProperty PortProperty = DependencyProperty.Register("Port", typeof(Int32), typeof(AddServerVM), new PropertyMetadata(443), CheckPort);

		static Boolean CheckPort(Object value) {
			return (Int32)value > 0 && (Int32)value < 65535;
		}

		public String ServerName {
			get { return (String)GetValue(ServerNameProperty); }
			set { SetValue(ServerNameProperty, value); }
		}
		public Int32 Port {
			get { return (Int32)GetValue(PortProperty); }
			set { SetValue(PortProperty, value); }
		}
		public void AddServer(Object obj) {
			if (String.IsNullOrEmpty(ServerName)) {
				Tools.MsgBox("Error", "The string must not be empty.", MessageBoxButton.OK, MessageBoxImage.Error);
				return;
			}
			MainWindowVM mwvm = (MainWindowVM)Application.Current.MainWindow.DataContext;
			if (mwvm.AddServerItem(ServerName.Replace("https://", null), Port)) { ServerName = String.Empty; }
		}
		Boolean CanAddServer(Object obj) {
			return !String.IsNullOrEmpty(ServerName) || !String.IsNullOrWhiteSpace(ServerName);
		}
	}
}